package com.wgts.wgts.mvp.view.activity.pay;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.ActivityStack;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.eventbus.PayEvenBus;
import com.wgts.wgts.control.eventbus.PayVipGoodEvent;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerPaySuccessComponent;
import com.wgts.wgts.dagger.module.PayResultModule;
import com.wgts.wgts.localbean.PayData;
import com.wgts.wgts.mvp.presenter.IPayResultPresenter;
import com.wgts.wgts.mvp.view.IPayResultView;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.mvp.view.activity.myorder.MyOrderActivity;
import com.wgts.wgts.mvp.view.activity.myorder.MyOrderDetailsActivity;
import com.wgts.wgts.netmodle.resp.SearchShopResultEntity;
import com.wgts.wgts.thirdParty.pay.PayTools;
import com.wgts.wgts.tools.AppInfor;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/*
 *
 *
 *   data:2018/9/2215:53
 *
 */public class PaySuccessActivity extends BaseActivity implements IPayResultView {
    @BindView(R.id.back_btn_image)
    ImageView backBtnImage;
    @BindView(R.id.back_btn_ly)
    LinearLayout backBtnLy;
    @BindView(R.id.include_title)
    TextView includeTitle;
    @BindView(R.id.include_text)
    TextView includeText;
    @BindView(R.id.include_right_image)
    ImageView includeRightImage;
    @BindView(R.id.include_right_ly)
    LinearLayout includeRightLy;
    @BindView(R.id.award_coin_text)
    TextView awardCoinText;
    @BindView(R.id.watch_award_ly)
    LinearLayout watchAwardLy;
    @BindView(R.id.watch_order_ly)
    LinearLayout watchOrderLy;
    @BindView(R.id.pay_result_recycle)
    RecyclerView payResultRecycle;
    @BindView(R.id.placeholder_text)
    TextView placeholderText;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private PayData payData;

    public static String PAY_TYPE = "";
    private String sPayType;
    private PayResultRecycleAdapter adapter;
    private List<SearchShopResultEntity.ShopList> shopLists;

    @Inject
    IPayResultPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_success_activity);
        ButterKnife.bind(this);
        includeTitle.setText("支付结果");
        Intent intent = getIntent();
        payData = intent.getParcelableExtra(PayTools.PAY_DATE);
        sPayType = intent.getStringExtra(PAY_TYPE);
        if (sPayType != null && sPayType.equals("ALIPAY")){
            presenter.affirmAlipay(payData.orderCode,"ALIPAY");
        }
        initView();
    }

    @Override
    public void init() {
        DaggerPaySuccessComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .payResultModule(new PayResultModule(this))
                .build()
                .inject(this);
        shopLists = new ArrayList<>();
    }

    private void initView() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(true);
        //load();
        switch (payData.orderType){
            case "NORMAL":
                awardCoinText.setText("订单编号：" + payData.orderCode);
                break;
            case "VIP":
                awardCoinText.setText("恭喜您成为了晒友盟会员");
                break;
            case "SVIP":
                awardCoinText.setText("恭喜您成为了晒友盟高级会员");
                break;
        }
        payResultRecycle.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PayResultRecycleAdapter(this, shopLists);
        payResultRecycle.setAdapter(adapter);
        //presenter.initList();
    }

    @OnClick({R.id.back_btn_ly, R.id.watch_award_ly, R.id.watch_order_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn_ly:
                close();
                break;
            case R.id.watch_award_ly:
                switch (payData.orderType){
                    case "NORMAL":
                        Intent intent = new Intent(PaySuccessActivity.this, MoHuMainActivity.class);
                        intent.putExtra(MoHuMainActivity.PAY_RESULT, 0);
                        startActivity(intent);
                        close();
                        break;
                    case "VIP":
                        Intent intent1 = new Intent(PaySuccessActivity.this, MoHuMainActivity.class);
                        intent1.putExtra(MoHuMainActivity.PAY_RESULT, 1);
                        startActivity(intent1);
                        close();
                        break;
                    case "SVIP":
                        Intent intent2 = new Intent(PaySuccessActivity.this, MoHuMainActivity.class);
                        intent2.putExtra(MoHuMainActivity.PAY_RESULT, 1);
                        startActivity(intent2);
                        close();
                        break;
                }
                break;
            case R.id.watch_order_ly:
                Intent intent1 = new Intent(PaySuccessActivity.this, MyOrderDetailsActivity.class);
                intent1.putExtra(MyOrderDetailsActivity.ORDER_ID, payData.orderId);
                intent1.putExtra(MyOrderDetailsActivity.PAY_STATUS, "ON_GOING");
                startActivity(intent1);
                close();
                break;
        }
    }

    @Override
    public void onError(int status, String msg) {

    }

    @Override
    public void initList(SearchShopResultEntity entity, boolean noMore) {
        shopLists.clear();
        adapter.setNoMore(noMore);
        shopLists.addAll(entity.list);
        if(noMore){
            refreshLayout.setEnableLoadmore(false);
        }
        else {
            refreshLayout.setEnableLoadmore(true);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void loadMore(SearchShopResultEntity entity, boolean noMore) {
        refreshLayout.finishLoadmore();
        adapter.setNoMore(noMore);
        if(noMore){
            refreshLayout.setEnableLoadmore(false);
        }
        else {
            refreshLayout.setEnableLoadmore(true);
        }
        shopLists.addAll(entity.list);
        adapter.notifyDataSetChanged();
    }

    private void load(){
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMore();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            close();
        }
        return super.onKeyDown(keyCode, event);
    }

    private void close(){
        if (payData.orderType != null && payData.orderType.equals("VIP")) {
            EventBus.getDefault().post(new PayVipGoodEvent());
            if (AppInfor.getInstance().getUserState() < 1) {
                AppInfor.getInstance().setUserState(1);
                for (Activity activity : ActivityStack.getInstance().getActivities()) {
                    if (activity != null && activity instanceof MoHuMainActivity) {
                        ((MoHuMainActivity) activity).UpdateSuccess();
                        EventBus.getDefault().post(new PayEvenBus());
                    }
                }
            }
        }
        if (payData.orderType != null && payData.orderType.equals("SVIP")) {
            EventBus.getDefault().post(new PayVipGoodEvent());
            if (AppInfor.getInstance().getUserState() < 2) {
                AppInfor.getInstance().setUserState(2);
                for (Activity activity : ActivityStack.getInstance().getActivities()) {
                    if (activity != null && activity instanceof MoHuMainActivity) {
                        ((MoHuMainActivity) activity).UpdateSuccess();
                    }
                }
            }
        }
        finish();
    }
}
